import pymysql

DB_pool={
    'local':{
        'host':'120.79.8.46',
        'user':'root',
        'pwd':'7CXYz#FT5*',
    }
}


def getIntance(link,database):
    db = pymysql.connect(host=DB_pool[link]['host'],user=DB_pool[link]['user'],passwd=DB_pool[link]['pwd'],db=database,charset='utf8mb4')
    return db

def insertdb(db,item):
    # 使用cursor()方法获取操作游标
    cursor = db.cursor()

    # SQL 插入语句
    sql = """INSERT INTO news (`news_url`,`news_title`,`news_info`)
         VALUES (%s,%s,%s)"""
    try:
        # 执行sql语句
        cursor.execute(sql,(item['news_url'],item['news_title'],item['news_info']))
        # 提交到数据库执行
        db.commit()
    except:
        # Rollback in case there is any error
        print ('插入数据失败!')
        db.rollback()